using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

using System.Data.SqlClient;

namespace Ddb_Demo
{
    public class SqlHelper
    {
        SqlConnection _connection;
        SqlCommand _command;
        SqlDataAdapter _adapter;

        public SqlHelper()
        {
            _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource"].ToString());
        }

        public bool ExecuteSql(string sql)
        {
            try
            {
                _connection.Open();
                _command = _connection.CreateCommand();
                _command.CommandType = System.Data.CommandType.Text;

                _command.CommandText = sql;

                _command.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (_connection.State == System.Data.ConnectionState.Open)
                    _connection.Close();
            }

            return true;
        }


        public DataTable ExecuteDataTable(string sql)
        {
            DataTable result = new DataTable();

            try
            {
                _connection.Open();
                _command = _connection.CreateCommand();
                _command.CommandText = sql;
                _adapter = new SqlDataAdapter(_command);
                _adapter.Fill(result);
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (_connection.State == ConnectionState.Open)
                    _connection.Close();
            }
            return result;
        }
    }
}
